package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Blog;
import com.hmdp.entity.User;
import com.hmdp.mapper.UserMapper;
import com.hmdp.service.IFollowService;
import com.hmdp.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private IFollowService followService;

    @Autowired
    private BlogServiceImpl blogService;

    @Override
    public Result queryUserById(Long id) {
        // 查询详情
        User user = getById(id);
        if (user == null) {
            return Result.ok();
        }
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        userDTO.setBlogs(queryBlogByUserId(user.getId()));
        // 返回
        return Result.ok(userDTO);
    }

    private List<Blog> queryBlogByUserId(Long userId) {
        return blogService.lambdaQuery()
            .eq(Blog::getUserId, userId)
            .list();
    }
}
